//
//  CustomDateActionSheet.m
//  CPICApp
//
//  Created by sinosoft on 13-3-11.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "CustomDateActionSheet.h"

@implementation CustomDateActionSheet
@synthesize CustomDateDelegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title{
	self = [super init];
    if (self) {
		int theight = height - 40;
		int btnnum = theight/50;
		for(int i=0; i<btnnum; i++){
			[self addButtonWithTitle:@" "];
		}
		toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
		toolBar.barStyle = UIBarStyleBlackOpaque;
		UIBarButtonItem *titleButton = [[UIBarButtonItem alloc] initWithTitle:title
                                                                        style:UIBarButtonItemStylePlain
                                                                       target:nil
                                                                       action:nil];
		
		UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                        style:UIBarButtonItemStyleDone
                                                                       target:self
                                                                       action:@selector(done)];
		
		UIBarButtonItem *leftButton  = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
                                                                        style:UIBarButtonItemStyleBordered
                                                                       target:self
                                                                       action:@selector(docancel)];
		
		UIBarButtonItem *fixedButton  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                      target:nil
                                                                                      action:nil];
		
		NSArray *array = [[NSArray alloc] initWithObjects:leftButton,fixedButton,titleButton,fixedButton,rightButton,nil];
		[toolBar setItems: array];
		[titleButton release];
		[leftButton  release];
		[rightButton release];
		[fixedButton release];
		[array       release];
		[self addSubview:toolBar];
		view = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, height-44)];
		view.backgroundColor = [UIColor groupTableViewBackgroundColor];
        

                
        DatepickerView=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
        DatepickerView.datePickerMode = UIDatePickerModeDateAndTime;
        
        NSDateFormatter *myFormatter=[[[NSDateFormatter alloc]init]autorelease];
        NSDate *minDate=[myFormatter dateFromString:@"1900-01-01 00:00:00 -0500"];
        NSDate *maxDate=[myFormatter dateFromString:@"2099-01-01 00:00:00 -0500"];
        
        DatepickerView.maximumDate=maxDate;
        DatepickerView.minimumDate=minDate;
        [view addSubview:DatepickerView];
        [self addSubview:view];
    }
    return self;
}
-(id)initWithDateOnlyActionHeight:(float)height WithSheetTitle:(NSString*)title
{
    self = [super init];
    if (self) {
		int theight = height - 40;
		int btnnum = theight/50;
		for(int i=0; i<btnnum; i++){
			[self addButtonWithTitle:@" "];
		}
		toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
		toolBar.barStyle = UIBarStyleBlackOpaque;
		UIBarButtonItem *titleButton = [[UIBarButtonItem alloc] initWithTitle:title
                                                                        style:UIBarButtonItemStylePlain
                                                                       target:nil
                                                                       action:nil];
		
		UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                        style:UIBarButtonItemStyleDone
                                                                       target:self
                                                                       action:@selector(done)];
		
		UIBarButtonItem *leftButton  = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
                                                                        style:UIBarButtonItemStyleBordered
                                                                       target:self
                                                                       action:@selector(docancel)];
		
		UIBarButtonItem *fixedButton  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                      target:nil
                                                                                      action:nil];
		
		NSArray *array = [[NSArray alloc] initWithObjects:leftButton,fixedButton,titleButton,fixedButton,rightButton,nil];
		[toolBar setItems: array];
		[titleButton release];
		[leftButton  release];
		[rightButton release];
		[fixedButton release];
		[array       release];
		[self addSubview:toolBar];
		view = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, height-44)];
		view.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        
        
        DatepickerView=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
        DatepickerView.datePickerMode = UIDatePickerModeDate;
        
        NSDateFormatter *myFormatter=[[NSDateFormatter alloc]init];
        NSDate *minDate=[myFormatter dateFromString:@"1900-01-01 00:00:00 -0500"];
        NSDate *maxDate=[myFormatter dateFromString:@"2099-01-01 00:00:00 -0500"];
        
        DatepickerView.maximumDate=maxDate;
        DatepickerView.minimumDate=minDate;
        [view addSubview:DatepickerView];
        [self addSubview:view];
        [myFormatter release];
    }
    return self;
}
-(void)done
{

    NSDate *pickerDate=[DatepickerView date];
    
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *destDateString=[dateFormatter stringFromDate:pickerDate];
    [dateFormatter release];
	[self dismissWithClickedButtonIndex:0 animated:YES];
    if ([self.CustomDateDelegate respondsToSelector:@selector(ClickDate:DateActionSheetTag:)]) {
        [self.CustomDateDelegate ClickDate:destDateString DateActionSheetTag:self.tag];
    }
    
}

-(void)docancel{
	[self dismissWithClickedButtonIndex:0 animated:YES];
}

-(void)dealloc{
    [toolBar release];
    [DatepickerView release];
	[view release];
	[super dealloc];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
